/*
 * ikakeibo 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.model;

import java.awt.Component;
import java.awt.FontMetrics;

import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

/**
 * @author wateray
 * @create 2009-5-10
 */
public class MemoReceiptTableCellRenderer extends DefaultTableCellRenderer{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /* (non-Javadoc)
     * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
     */
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
     super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
     TableColumn columnMemo = table.getColumnModel().getColumn(column);
     Border border = this.getBorder();
     int margin = 0;
     if(border != null){
             margin =  border.getBorderInsets(this).left + border.getBorderInsets(this).right;
     }
   
     FontMetrics fm = table.getFontMetrics(table.getFont());
     int fontWidth = fm.stringWidth(value.toString());
    
     margin = margin == 0? 1:margin;
    
//     System.out.println("columnWidth="+columnMemo.getWidth()+ " fontWidth=" + fontWidth+ "margin=" + margin);
     if(columnMemo.getWidth() <= fontWidth + margin){
         //setToolTipText
            this.setToolTipText(value.toString());
     }
     else{
             this.setToolTipText(null);
     }

            return this;
    }

}

